package com.zy.cloud.controller;

import com.zy.cloud.entity.CommonResult;
import com.zy.cloud.entity.User;
import com.zy.cloud.lb.MyLoadBalancer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.net.URI;
import java.util.List;

/**
 * @ClassName: UserController
 * @Description: #TODO
 * @Author: yanzy
 * @Date: 2021/3/1 23:08
 */
@RestController
@Slf4j
public class UserController {

    @Resource
    private RestTemplate restTemplate;

    private static final String URL = "http://CLOUD-PROVIDER-SERVER";

    @Resource
    private DiscoveryClient discoveryClient;

    @Resource
    private MyLoadBalancer myLoadBalancer;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/user/{id}")
    public CommonResult<User> selectOne(@PathVariable("id") Long id) {

        return restTemplate.getForObject(URL + "/user/" + id, CommonResult.class);
    }

    @GetMapping("/user/getForEntity/{id}")
    public CommonResult<User> selectOne2(@PathVariable("id") Long id) {

        ResponseEntity<CommonResult> forEntity = restTemplate.getForEntity(URL + "/user/" + id, CommonResult.class);
        if (forEntity.getStatusCode().is2xxSuccessful()) {
            return forEntity.getBody();
        } else {
            return new CommonResult<>(400, "操作失败");
        }
    }

    /**
     * 服务发现
     *
     * @return
     */
    @GetMapping("/discovery")
    public Object discovery() {

        List<String> services = discoveryClient.getServices();
        for (String element : services) {
            log.info("服务element: " + element);
        }

        List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PROVIDER-SERVER");
        for (ServiceInstance instance : instances) {
            log.info(instance.getServiceId() + "\t" + instance.getHost() + "\t" + instance.getPort() + "\t" + instance.getUri());
        }

        return this.discoveryClient;
    }

    /**
     * 自定义负载均衡算法实现，测试该方法时需要注释掉 RestTemplate注入时的@LoadBalancer开启负载均衡注解
     * @param id
     * @return
     */
    @GetMapping("/mylb/user/{id}")
    public CommonResult<User> selectOneByMyLb(@PathVariable("id") Long id) {

        List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PROVIDER-SERVER");
        if (instances == null || instances.size() <= 0) {
            return new CommonResult<>(200, "当前无服务提供者");
        }
        ServiceInstance serviceInstance = myLoadBalancer.instances(instances);
        URI uri = serviceInstance.getUri();
        return restTemplate.getForObject(uri + "/user/" + id, CommonResult.class);
    }
}
